package com.iwayin.web.action.ask;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.iwayin.core.bo.ask.AskBO;
import com.iwayin.core.dataobject.AskDO;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.exception.BOException;
import com.iwayin.core.utils.imagesearch.BImageSearch;
import com.iwayin.core.utils.imagesearch.ImageDO;
import com.iwayin.web.action.BaseAction;
import com.iwayin.web.common.AjaxResult;
import com.opensymphony.xwork2.ActionContext;

public class AskAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2585727175179805172L;

	private AskDO query;
	private ImageDO imageQuery;
	private List<AskDO> askList;
	private AjaxResult ajaxResult;

	private AskBO askBO;

	public String askInit() {
		return SUCCESS;
	}

	/**
	 * bing图片搜索
	 * 
	 * @return
	 */
	public String bingImageSearch() {
		if (imageQuery == null) {
			imageQuery = new ImageDO();
		}
		imageQuery.setPerPageSize(4);
		List<ImageDO> imageList = BImageSearch.search(imageQuery);
		ActionContext.getContext().put("imageList", imageList);
		return SUCCESS;
	}

	/**
	 * 创建问题
	 * 
	 * @return
	 */
	public String createAsk() {
		try {
			query.setUserId(this.getLoginUser().getId());
			String savePath = ServletActionContext.getServletContext().getRealPath("/askPic"); // 获取项目根路径
			File uploadDir = new File(savePath);
			if (!uploadDir.exists()) {
				uploadDir.mkdirs();
			}
			query.setTempPicPath(savePath);
			this.askBO.addAsk(query);
			ajaxResult = AjaxResult.succResult();
		} catch (BOException e) {
			ajaxResult = AjaxResult.errorResult(e.getMessage());
		} catch (Exception e) {
			ajaxResult = AjaxResult.errorResult("系统异常");
		}
		return SUCCESS;
	}

	/**
	 * 删除ask
	 * 
	 * @return
	 */
	public String deleteAsk() {
		// 参数校验
		if (query == null || query.getId() == null) {
			ajaxResult = AjaxResult.errorResult("系统异常");
			return SUCCESS;
		}
		try {
			query.setUserId(this.getLoginUser().getId());
			this.askBO.deleteAsk(query);
			ajaxResult = AjaxResult.succResult();
		} catch (BOException e) {
			ajaxResult = AjaxResult.errorResult(e.getMessage());
		} catch (Exception e) {
			ajaxResult = AjaxResult.errorResult("系统错误");
		}
		return SUCCESS;
	}

	/**
	 * 列出用户的ask
	 * 
	 * @return
	 */
	public String findUserAsk() {
		UserDO loginUser = this.getLoginUser();
		if (loginUser != null) {
			query.setLoginId(loginUser.getId());
		}
		query.setPerPageSize(12);
		askList = askBO.findUserAsks(query);
		return SUCCESS;
	}

	/**
	 * 未登录个人页ask
	 * 
	 * @return
	 */
	public String findNoLoginUserAsk() {
		query.setPerPageSize(12);
		askList = askBO.findNoLoginUserAsk(query);
		return SUCCESS;
	}

	public String askDetails() {
		askList = askBO.prevNextAskDetails(query.getId());
		if (this.getLoginUser() != null) {
			query = askBO.askDetails(query.getId(), this.getLoginUser().getId());
			return SUCCESS;
		} else {
			query = askBO.askDetails(query.getId());
			return "nologin";
		}
	}

	/**
	 * @descriptions 查询follow的问题列表
	 * @return
	 */
	public String findFollowAsk() {
		if (query == null) {
			query = new AskDO();
		}
		query.setPerPageSize(12);
		query.setUserId(this.getLoginUser().getId());
		askList = this.askBO.findFollowAsk(query);
		return SUCCESS;
	}

	public String findHotAsk() {
		if (query == null) {
			query = new AskDO();
		}
		query.setPerPageSize(12);
		query.setUserId(this.getLoginUser().getId());
		askList = this.askBO.findHotAsk(query);
		return SUCCESS;
	}

	public String findLatestAsk() {
		if (query == null) {
			query = new AskDO();
		}
		query.setPerPageSize(12);
		query.setUserId(this.getLoginUser().getId());
		askList = this.askBO.findLastestAsk(query);
		return SUCCESS;
	}

	public String findNoLoginLastestAsk() {
		if (query == null) {
			query = new AskDO();
		}
		query.setPerPageSize(12);
		askList = this.askBO.findNoLoginLastestAsk(query);
		return SUCCESS;
	}

	public void setAskBO(AskBO askBO) {
		this.askBO = askBO;
	}

	public List<AskDO> getAskList() {
		return askList;
	}

	public AskDO getQuery() {
		return query;
	}

	public void setQuery(AskDO query) {
		this.query = query;
	}

	public AjaxResult getAjaxResult() {
		return ajaxResult;
	}

	public ImageDO getImageQuery() {
		return imageQuery;
	}

	public void setImageQuery(ImageDO imageQuery) {
		this.imageQuery = imageQuery;
	}
}
